package com.jcake.vimlike.commands;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.jface.dialogs.PopupDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.KeyAdapter;
import org.eclipse.swt.events.KeyEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.ui.handlers.HandlerUtil;

import com.jcake.vimlike.api.ICommand;
import com.jcake.vimlike.internal.CommandManager;
import com.jcake.vimlike.internal.NoSuitableCommandException;

public class TypeVimCommandHander extends AbstractHandler {

	public Object execute(final ExecutionEvent event) throws ExecutionException {
		final IWorkbenchWindow activeWorkbenchWindow = HandlerUtil.getActiveWorkbenchWindow(event);
		PopupDialog dialog = new PopupDialog(HandlerUtil.getActiveShell(event), SWT.NONE, true, false, false, false, false, null, null) {
			@Override
			protected Control createDialogArea(Composite parent) {
				Composite panel = new Composite(parent, SWT.NONE);
				panel.setLayout(new GridLayout(1, false));
				final Text text = new Text(panel, SWT.SINGLE);
				GridData layoutData = new GridData(SWT.FILL, SWT.NONE, true, false);
				layoutData.widthHint = 500;
				text.setLayoutData(layoutData);
				text.setFocus();
				text.addKeyListener(new KeyAdapter() {
					@Override
					public void keyReleased(KeyEvent e) {
						String command = text.getText();
						command = command.trim();
						switch (e.keyCode) {
							case 27:
								break;
							case '\r':
								if (command.length() > 0 && (command.charAt(0) == ':' || command.charAt(0) == '/')) {
									maybeExecuteCommand(command);
								} else {
									close();
								}
								break;
							case '\b':
								if (command.length() == 0 || (command.length() > 0 && (command.charAt(0) != ':' && command.charAt(0) != '/'))) {
									close();
								}
								break;
							default:
								///System.out.println(e.keyCode);
								if (command.length() > 0 && (command.charAt(0) != ':' && command.charAt(0) != '/')) {
									maybeExecuteCommand(command);
								}
						}
					}
				});
				return panel;
			}

			@Override
			protected Point getDefaultLocation(Point initialSize) {
				if (activeWorkbenchWindow == null) {
					return super.getDefaultLocation(initialSize);
				}
				return new Point(activeWorkbenchWindow.getShell().getLocation().x + 5, activeWorkbenchWindow.getShell().getSize().y - initialSize.y - 5);
			}

			private void maybeExecuteCommand(String command) {
				try {
					ICommand commandToExecute = CommandManager.getInstance().findCommand(command);
					if (commandToExecute != null) {
						// System.out.println("Found command " + command);
						close();
						commandToExecute.execute(command);
					}
				} catch (NoSuitableCommandException e) {
					close();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}

		};
		dialog.open();

		return null;
	}

}